// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const tencentcloud = require("tencentcloud-sdk-nodejs");


const IaiClient = tencentcloud.iai.v20180301.Client;
const models = tencentcloud.iai.v20180301.Models;

const Credential = tencentcloud.common.Credential;
const ClientProfile = tencentcloud.common.ClientProfile;
const HttpProfile = tencentcloud.common.HttpProfile;

let cred = new Credential("AKIDTrzu41zaKo23qNAaGfohLjrjSwB9CRr1", "76pIVSFn8DSRBftzjW3KM9gxXrwlJckc");
let httpProfile = new HttpProfile();
httpProfile.endpoint = "iai.tencentcloudapi.com";
let clientProfile = new ClientProfile();
clientProfile.httpProfile = httpProfile;
let client = new IaiClient(cred, "ap-guangzhou", clientProfile);

let req = new models.CompareFaceRequest();



// 云函数入口函数
exports.main = async (event, context) => {
  console.log('event', event)
  return new Promise((resolve, reject) => {
    let firstUrl =  event.firstAvatarUrl;
    let secondUrl = event.secondAvatarUrl;

    let params = `{"UrlA":"${firstUrl}","UrlB":"${secondUrl}","FaceModelVersion":"3.0"}`
    req.from_json_string(params);

    client.CompareFace(req, function (errMsg, response) {

      if (errMsg) {
        console.log(errMsg);
        reject(errMsg)
        return;
      }

      console.log(response.to_json_string());
      resolve(response)
    });

  })
}